//
//  DietViewController.m
//  muscleLife
//
//  Created by xalo on 16/4/29.
//  Copyright © 2016年 HeavenAndHell. All rights reserved.
//

#import "DietViewController.h"
#import "CategoryViewController.h"
#import "DietTableViewCell.h"
#import <MobAPI/MobAPI.h>
#define KModelkey @"categoryInfo"
#define kChildsArrKey @"childs"

@interface DietViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)NSMutableArray *dataSource;//存放数组模型
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic, retain)CategoryRootModel* rootMenuModel;

@end

@implementation DietViewController

#pragma mark - 模型数据的懒加载
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:self.view.frame];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.bounces = NO;
    }
    return _tableView;
}
-(CategoryRootModel *)rootMenuModel{
    
    if (!_rootMenuModel) {
        _rootMenuModel = [[CategoryRootModel alloc] init];
    }
    return _rootMenuModel;
}


//数据源的懒加载
-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource=[NSMutableArray  array];
    }
    return _dataSource;
}

#pragma mark- 视图的生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"DietTableViewCell" bundle:nil] forCellReuseIdentifier:@"DietTableViewCell"];
    [self acquireSourceData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


#pragma mark----TableView的代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

//定义Cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DietTableViewCell  *cell=[tableView dequeueReusableCellWithIdentifier:@"DietTableViewCell" forIndexPath:indexPath];
    CategoryRootModel *rootModel=self.dataSource[indexPath.row];
    [cell setCellWithModel:rootModel];
    switch (indexPath.row) {
        case 0:
            cell.cellImageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"dishes0"]];
            break;
        case 1:
            cell.cellImageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"craft2"]];
            break;
        case 2:
            cell.cellImageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"cuisine14"]];
            break;
        case 3:
            cell.cellImageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"crowd8"]];
            break;
        case 4:
            cell.cellImageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"function0"]];
            break;
        default:
            break;
    }
    
    return cell;
}

//cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}

//点击cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CategoryViewController *categoryVC=[[UIStoryboard storyboardWithName:@"Heath" bundle:nil]instantiateViewControllerWithIdentifier:@"CategoryViewController"];
    categoryVC.model = [self.dataSource objectAtIndex:indexPath.row];
    categoryVC.indexOfImage = indexPath.row;
    [self.navigationController pushViewController:categoryVC animated:YES];
}

//数据请求
-(void)acquireSourceData{
    [MobAPI sendRequest:[MOBACookRequest categoryRequest] onResult:^(MOBAResponse *response) {
        if (response.error) {
            NSLog(@"error=%@",response.error);
        }else{
            NSDictionary *result=response.responder;
            [self.rootMenuModel setValuesForKeysWithDictionary:result];
            for (int i = 0; i < self.rootMenuModel.childs.count; i++) {
                CategoryRootModel* model = [[CategoryRootModel alloc] init];
                [model setValuesForKeysWithDictionary:self.rootMenuModel.childs[i]];
                [self.dataSource addObject:model];
            }

            [self.tableView reloadData];
        }
    }];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
